package kr.hkit.score_pjt;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import kr.hkit.framework.dao.LoadPropertyDAO;
import kr.hkit.framework.inputPanel.ValidCheck;
import kr.hkit.framework.inputPanel.inputTxtPanel;
import kr.hkit.score_pjt.DAO.StdInfoDao;
import kr.hkit.score_pjt.DTO.StdInfo;
import kr.hkit.score_pjt.ListUI.ScoreListUI;

public class StudentScoreMgn extends JFrame implements ActionListener {
	private JPanel pMain;
	private JButton btnInput;
	private JButton btnOutput;
	private JButton btnDelete;
	private inputTxtPanel pName;
	private inputTxtPanel pNum;
	private inputTxtPanel pKor;
	private inputTxtPanel pEng;
	private inputTxtPanel pMath;

	private Connection con;
	private StdInfoDao dao;
	
	private ScoreListUI output;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Connection con = LoadPropertyDAO.getConnection();
					StudentScoreMgn frame = new StudentScoreMgn(con);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @param con 
	 */
	public StudentScoreMgn(Connection con) {
		this.con = con;
		dao = new StdInfoDao(con);
		initialize();
	}


	private void initialize() {
		setTitle("학생별 성적처리 프로그램");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		JPanel contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		pMain = new JPanel();
		contentPane.add(pMain, BorderLayout.CENTER);
		pMain.setLayout(new GridLayout(0, 2, 5, 5));
		
		pNum = new inputTxtPanel("학번");
		pMain.add(pNum);
		
		pName = new inputTxtPanel("성명");
		pName.getTfValue().addFocusListener(new FocusAdapter(){

			@Override
			public void focusGained(FocusEvent e) {
				super.focusGained(e);
				try {
					ValidCheck.isValidValue(pNum, 3);
					int num = Integer.parseInt(pNum.getTfValue().getText().trim());
					String name = dao.selectStdNameByStdnum(num);
					pName.getTfValue().setText(name);
					pKor.getTfValue().requestFocus();
					//System.out.println(num);
				} catch (Exception e1) {
					pNum.getTfValue().requestFocus();
					pNum.getTfValue().selectAll();
					JOptionPane.showMessageDialog(null, e1.getMessage());
				}
			}
		
		});
		pMain.add(pName);
		
		pKor = new inputTxtPanel("국어점수");
		pMain.add(pKor);
		
		JPanel panel_3 = new JPanel();
		pMain.add(panel_3);
		
		pEng = new inputTxtPanel("영어점수");
		pMain.add(pEng);
		
		JPanel panel_5 = new JPanel();
		pMain.add(panel_5);
		
		pMath = new inputTxtPanel("수학점수");
		pMain.add(pMath);
		
		JPanel panel_7 = new JPanel();
		pMain.add(panel_7);
		
		JPanel pBtn = new JPanel();
		contentPane.add(pBtn, BorderLayout.SOUTH);
		
		btnInput = new JButton("입력");
		btnInput.addActionListener(this);
		pBtn.add(btnInput);
		
		btnOutput = new JButton("출력");
		btnOutput.addActionListener(this);
		pBtn.add(btnOutput);
		
		btnDelete = new JButton("전체삭제");
		btnDelete.addActionListener(this);
		pBtn.add(btnDelete);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnDelete) {
			actionPerformedBtnDelete(e);
		}
		if (e.getSource() == btnOutput) {
			actionPerformedBtnOutput(e);
		}
		if (e.getSource() == btnInput) {
			actionPerformedBtnInput(e);
		}
	}
	
	
	
	protected void actionPerformedBtnInput(ActionEvent e) {
		try {
			validCheck();
			StdInfo std = getData();
			dao.insertProduct(std);
			//System.out.println(std);
			if(output != null){
				ArrayList<StdInfo> list = dao.selectAllStdInfoList();
				output.refreshData(list);
			}
			JOptionPane.showMessageDialog(null, "완료\n" + std);
			clearTf();
		} catch (Exception e1) {
			JOptionPane.showMessageDialog(null, e1.getMessage());
		}
	}
	
	private void clearTf() {
		pNum.getTfValue().setText("");
		pName.getTfValue().setText("");
		pKor.getTfValue().setText("");
		pEng.getTfValue().setText("");
		pMath.getTfValue().setText("");	
	}

	private StdInfo getData() {
		int stdNum = Integer.parseInt(pNum.getTfValue().getText().trim());
		String stdName = pName.getTfValue().getText().trim();
		int korScore = Integer.parseInt(pKor.getTfValue().getText().trim());
		int engScore = Integer.parseInt(pEng.getTfValue().getText().trim());
		int mathScore = Integer.parseInt(pMath.getTfValue().getText().trim());
		return new StdInfo(stdNum, stdName, korScore, engScore, mathScore);
	}

	private void validCheck() throws Exception {
		ValidCheck.isValidValue(pNum);
		ValidCheck.isValidValue(pName);
		ValidCheck.isValidValue(pKor, 3, 100);
		ValidCheck.isValidValue(pEng, 3, 100);
		ValidCheck.isValidValue(pMath, 3, 100);
	}
	
	protected void actionPerformedBtnOutput(ActionEvent e) {
		try {
			ArrayList<StdInfo> list = dao.selectAllStdInfoList();
			output = new ScoreListUI("총점평균별 석차 및 등급", list);
			output.setVisible(true);
		} catch (Exception e1) {
			JOptionPane.showMessageDialog(null, e1.getMessage());
			//e1.printStackTrace();
		}
	}
	
	protected void actionPerformedBtnDelete(ActionEvent e) {
		try {
			dao.deleteAllStdInfo();
			JOptionPane.showMessageDialog(null, "삭제완료");
			if(output != null){
				output.setVisible(false);
			}
		} catch (SQLException e1) {
			e1.printStackTrace();
		}		
	}
}
